﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace EvoBoost.Core
{
	[Serializable]
	public class IndexedFeatureSplit : SplitBase<double[]>
	{
		private readonly int featureCount;

		private IndexedFeatureSplit(int featureCount)
			: base(new List<ISplitParameterDescription> { new RandomSplitParameterDescription(featureCount) })
		{
			Debug.Assert(featureCount > 0);
			this.featureCount = featureCount;
		}

		public static IndexedFeatureSplit Create(int featureCount)
		{
			if (featureCount <= 0)
				throw new ArgumentOutOfRangeException("featureCount", "Feature count should be positive.");

			return new IndexedFeatureSplit(featureCount);
		}

		public override double Calculate(double[] sampleProperties, IList<double> parameterValues)
		{
			if (sampleProperties == null) throw new ArgumentNullException("sampleProperties");
			if (parameterValues == null) throw new ArgumentNullException("parameterValues");
			if (parameterValues.Count != 1)
				throw new ArgumentException("Only one parameter value representing feature index should be given.", "parameterValues");
			if (sampleProperties.Length != this.featureCount)
				throw new ArgumentException("Sample properties of invalid length given.", "sampleProperties");

			int featureIndex = (int) parameterValues[0];
			if (featureIndex < 0 || featureIndex >= this.featureCount)
				throw new ArgumentException("Invalid feature index given.", "parameterValues");

			return sampleProperties[featureIndex];
		}
	}
}
